%%%%基于16*32基矩阵生成Z=360,180,90,45都不含4,6环的矩阵
clc;
clear all;
close all;

hmx_1d4 = [ 293, 442,  -1,  -1,  37,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
             -1,  -1,  -1,  64,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
             -1,  -1,  -1,   0,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
            346, 133,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
             -1, 218, 384,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1;
             -1,  -1,  -1, 421,   0,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1;
             -1,  74,  96,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1;
             -1, 250,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1;
             -1,  -1,  -1, 272,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1;
             -1,  -1, 373, 384,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1;
            208, 261,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0;
             -1,  -1,  -1, 138,  37,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0;];
Z_Max_1d4 = 512;

hmx_1d3_o1 = [   -1,  -1,  -1,  -1, 388,  -1, 218,  73,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1, 260,  -1,  -1,  -1,  -1, 397, 194,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
             470,  -1,  -1,  -1,  -1, 456, 170, 156,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1,  -1,  -1,  -1,  83,  -1, 345, 295,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1,  -1, 188,  -1,  -1,  -1, 268, 470,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1,  -1,  -1, 156,  -1,  -1,  41,  62,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1, 280,  -1,  -1,  -1,  -1,  65, 321,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1,  -1, 475,  -1,  -1, 153, 459, 175,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1,  -1,  -1,  -1,  -1,  -1, 128, 410, 380,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1;
              -1,  -1,  -1, 283,  -1,  -1, 494, 331,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1;
              -1,  -1, 185,  -1,  -1,  -1, 226, 257,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1;
             509,  -1,  -1,  -1,  -1,  -1, 466,  37,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1;
              -1,  -1,  -1,  -1, 369,  -1, 508, 386,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1;
              -1,  -1,  -1,  48,  -1, 315, 352, 265,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1;
              -1, 392,  -1,  -1,  -1,  -1, 235, 372,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0;
             141,  -1,  -1,  -1,  -1,  -1, 473, 125,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0;];
         
hmx_1d3_o2 = [   -1,  -1,  -1,  -1, 101,  -1, 253,  15,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
            -1, 334,  -1,  -1,  -1,  -1, 171, 369,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
            470,  -1,  -1,  -1,  -1, 456, 170, 156,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
            -1,  -1,  -1,  -1, 122,  -1, 220, 318,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
            -1,  -1, 133,  -1,  -1,  -1, 251,  63,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
            -1,  -1,  -1, 418,  -1,  -1, 262,  93,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
            -1, 280,  -1,  -1,  -1,  -1, 386, 352,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
            -1,  -1, 475,  -1,  -1, 153, 459, 175,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
            -1,  -1,  -1,  -1,  -1,  -1, 335, 383, 380,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,
            -1,  -1,  -1, 283,  -1,  -1, 213, 205,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,
            -1,  -1, 185,  -1,  -1,  -1, 255, 421,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,
            270,  -1,  -1,  -1,  -1,  -1, 466,  37,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,
            -1,  -1,  -1,  -1, 369,  -1, 508, 396,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,
            -1,  -1,  -1,  48,  -1, 315, 352, 265,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,
            -1, 392,  -1,  -1,  -1,  -1, 235, 372,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,
            478,  -1,  -1,  -1,  -1,  -1, 473, 291,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0];
        
 hmx_1d3 = [  -1  -1  -1  -1 323  -1 255 257   0   0  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1;
              -1 337  -1  -1  -1  -1 179 312  -1   0   0  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1;
             470  -1  -1  -1  -1 486 170 156  -1  -1   0   0  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1;
              -1  -1  -1  -1 370  -1 231 395  -1  -1  -1   0   0  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1;
              -1  -1 211  -1  -1  -1  56 290  -1  -1  -1  -1   0   0  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1;
              -1  -1  -1 139  -1  -1 161 184  -1  -1  -1  -1  -1   0   0  -1  -1  -1  -1  -1  -1  -1  -1  -1;
              -1 221  -1  -1  -1  -1 485  17  -1  -1  -1  -1  -1  -1   0   0  -1  -1  -1  -1  -1  -1  -1  -1;
              -1  -1 475  -1  -1 153 459 175  -1  -1  -1  -1  -1  -1  -1   0   0  -1  -1  -1  -1  -1  -1  -1;
              -1  -1  -1  -1  -1  -1 310 116 380  -1  -1  -1  -1  -1  -1  -1   0   0  -1  -1  -1  -1  -1  -1;
              -1  -1  -1 263  -1  -1 300  67  -1  -1  -1  -1  -1  -1  -1  -1  -1   0   0  -1  -1  -1  -1  -1;
              -1  -1 130  -1  -1  -1 211 319  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1   0   0  -1  -1  -1  -1;
             236  -1  -1  -1  -1  -1 218 139  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1   0   0  -1  -1  -1;
              -1  -1  -1  -1 369  -1 307  53  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1   0   0  -1  -1;
              -1  -1  -1  48  -1 315 352 265  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1   0   0  -1;
              -1 392  -1  -1  -1  -1 403 178  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1   0   0;
             509  -1  -1  -1  -1  -1   9 399   0  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1   0;];
  
Z_Max_1d3 = 512;

hmx_1d2 = [   -1, 501, 389,  -1,  -1,  -1,  -1,  -1, 293, 442,  -1,  -1,  37,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1, 144,  -1,  -1,  -1, 117, 421,  48,  -1,  -1,  -1,  64,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1,  -1,  -1, 128, 117, 432,  -1, 176,  -1,  -1,  -1,   0,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
             325,  -1, 250,  -1,  -1,  -1,  -1,  -1, 346, 133,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1,  -1;
              -1,  -1, 208,  -1,  -1,  -1, 448,  -1,  -1, 218, 384,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1,  -1;
              -1,  -1,  -1,  -1, 245, 213,  -1, 437,  -1,  -1,  -1, 421,   0,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1,  -1;
              -1,  -1, 506, 282,  -1,  -1,  -1,  -1,  -1,  74,  96,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1,  -1;
              -1,  58, 389,  -1,  -1,  -1,  10,  -1,  -1, 250,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1,  -1;
              64,  -1,  -1,  -1, 442, 128,  -1, 229,  -1,  -1,  -1, 272,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1,  -1;
              -1,  -1,  -1,  -1,  -1, 501,  -1, 314,  -1,  -1, 373, 384,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,  -1;
              -1,  -1,  37, 346,  -1,  -1,  -1,  -1, 208, 261,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0;
             229,  -1,  -1,  -1,  -1, 352,  -1, 218,  -1,  -1,  -1, 138,  37,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0;];
Z_Max_1d2 = 512;

WiMax_ldpc_2d3B=[    10  -1 101  -1 250  -1 256  -1 192  -1 437  -1 250  -1  80  -1 506   0  -1  -1  -1  -1  -1  -1;
                     -1 368  -1 469  -1 176  -1  16  -1  85  -1 197  -1 213  -1 256  -1   0   0  -1  -1  -1  -1  -1;
                     53  -1 458  -1 330  -1 149  -1 453  -1  85  -1 181  -1 389  -1  -1  -1   0   0  -1  -1  -1  -1;
                     -1 149  -1 170  -1 432  -1 144  -1 469  -1  26  -1 298  -1 197  -1  -1  -1   0   0  -1  -1  -1;
                    122  -1 154  -1  80  -1 160  -1 352  -1 128  -1 266  -1 330  -1  -1  -1  -1  -1   0   0  -1  -1;
                     -1 160  -1 346  -1 288  -1  74  -1   0  -1 160  -1 394  -1   0  -1  -1  -1  -1  -1   0   0  -1;
                    170  -1   0  -1  80  -1 298  -1 453  -1  26  -1  32  -1 277  -1   0  -1  -1  -1  -1  -1   0   0;
                     -1   0  -1 250  -1  69  -1 325  -1 448  -1 293  -1 416  -1 218 506  -1  -1  -1  -1  -1  -1   0;];
hmx_2d3B_n1 = [     242  -1 277  -1  15  -1 205  -1 296  -1  27  -1 303  -1 423  -1 210   0  -1  -1  -1  -1  -1  -1;
                     -1 142  -1  40  -1  28  -1  67  -1 249  -1   1  -1 302  -1 400  -1   0   0  -1  -1  -1  -1  -1;
                    374  -1 100  -1 377  -1 405  -1  12  -1 134  -1 260  -1 440  -1  -1  -1   0   0  -1  -1  -1  -1;
                     -1 405  -1  29  -1 456  -1 306  -1 432  -1 178  -1 445  -1 369  -1  -1  -1   0   0  -1  -1  -1;
                    390  -1  21  -1 216  -1  37  -1  12  -1 456  -1 463  -1 400  -1  -1  -1  -1  -1   0   0  -1  -1;
                     -1 146  -1 187  -1 475  -1  89  -1 351  -1 169  -1  85  -1 245  -1  -1  -1  -1  -1   0   0  -1;
                    160  -1 281  -1 148  -1 275  -1  57  -1 481  -1 349  -1  96  -1 268  -1  -1  -1  -1  -1   0   0;
                     -1 418  -1 236  -1 275  -1 277  -1 178  -1 326  -1 296  -1 307 210  -1  -1  -1  -1  -1  -1   0;];
hmx_2d3B_n2 = [     131  -1 338  -1 207  -1 363  -1 332  -1 276  -1 150  -1 483  -1 506   0  -1  -1  -1  -1  -1  -1;
                     -1 163  -1 167  -1  55  -1 449  -1 297  -1 217  -1 436  -1  41  -1   0   0  -1  -1  -1  -1  -1;
                    430  -1 188  -1 409  -1 173  -1 461  -1 377  -1 327  -1 266  -1  -1  -1   0   0  -1  -1  -1  -1;
                     -1 200  -1 417  -1 275  -1 255  -1 190  -1 364  -1 494  -1 407  -1  -1  -1   0   0  -1  -1  -1;
                    122  -1 154  -1 292  -1 160  -1 352  -1 128  -1 266  -1 330  -1  -1  -1  -1  -1   0   0  -1  -1;
                     -1  24  -1  65  -1  71  -1 228  -1 119  -1 476  -1 128  -1 476  -1  -1  -1  -1  -1   0   0  -1;
                    170  -1   0  -1  80  -1 298  -1 453  -1  26  -1  32  -1 277  -1   0  -1  -1  -1  -1  -1   0   0;
                     -1 421  -1  80  -1 105  -1 324  -1 456  -1  78  -1  35  -1 341 506  -1  -1  -1  -1  -1  -1   0;];
                 
 hmx_2d3 = [ 100  -1  70  -1 223  -1 173  -1 505  -1 210  -1 195  -1 214  -1 506   0  -1  -1  -1  -1  -1  -1;
              -1 217  -1 270  -1  65  -1 294  -1 404  -1 444  -1 369  -1 390  -1   0   0  -1  -1  -1  -1  -1;
              53  -1 333  -1 344  -1 149  -1 300  -1 174  -1 237  -1 423  -1  -1  -1   0   0  -1  -1  -1  -1;
              -1  16  -1 170  -1  17  -1 503  -1 193  -1  26  -1 298  -1 256  -1  -1  -1   0   0  -1  -1  -1;
             122  -1 154  -1 411  -1 160  -1 352  -1 128  -1 266  -1 330  -1  -1  -1  -1  -1   0   0  -1  -1;
              -1 160  -1 346  -1 288  -1  74  -1   0  -1 160  -1 394  -1   0  -1  -1  -1  -1  -1   0   0  -1;
             170  -1   0  -1  80  -1 298  -1 453  -1  26  -1  32  -1 277  -1   0  -1  -1  -1  -1  -1   0   0;
              -1   0  -1 295  -1  69  -1 325  -1 448  -1  53  -1 416  -1 386 506  -1  -1  -1  -1  -1  -1   0;];
  
ZWiMax_ldpc_2d3B = 512;

WiMax_ldpc_3d4B=[     -1, 432,  -1, 149,  -1,  -1,  74, 133,  90,  -1,  -1, 453, 154, 277, 416, 506, 117, 490,   0,   0,  -1,  -1,  -1,  -1;
                     224,  -1,  74, 362, 170,  -1,  -1,  -1,  -1, 373, 229,  58, 192, 213, 176, 304, 202, 128,  -1,   0,   0,  -1,  -1,  -1;
                      -1,  -1, 106,  -1,  -1, 336, 208,  -1, 373, 357,  -1, 202,  21, 384, 250, 154, 320,  26, 426,  -1,   0,   0,  -1,  -1;
                     341,  10,  -1,  -1, 336,  -1,  -1,  16, 272,  -1, 432,  80, 501,  48, 453, 192,  74, 101,  -1,  -1,  -1,   0,   0,  -1;
                      -1, 282, 320, 426,  -1, 138, 400,  -1,  -1,  -1,  -1, 458, 410,   5,  16, 384, 320, 133,  -1,  -1,  -1,  -1,   0,   0;
                     410,  -1,  -1,  -1,  80, 149,  -1, 186,  -1, 384, 160, 362, 453, 448, 138, 341,  58, 474,   0,  -1,  -1,  -1,  -1,   0;];
ZWiMax_ldpc_3d4B = 512;

shorted_3d4B=[   74, 133,  90,  -1,  -1, 453, 154, 277, 416, 506, 117, 490,   0,   0,  -1,  -1,  -1,  -1;
                 -1,  -1,  -1, 373, 229,  58, 192, 213, 176, 304, 202, 128,  -1,   0,   0,  -1,  -1,  -1;
                208,  -1, 373, 357,  -1, 202,  21, 384, 250, 154, 320,  26, 426,  -1,   0,   0,  -1,  -1;
                 -1,  16, 272,  -1, 432,  80, 501,  48, 453, 192,  74, 101,  -1,  -1,  -1,   0,   0,  -1;
                400,  -1,  -1,  -1,  -1, 458, 410,   5,  16, 384, 320, 133,  -1,  -1,  -1,  -1,   0,   0;
                 -1, 186,  -1, 384, 160, 362, 453, 448, 138, 341,  58, 474,   0,  -1,  -1,  -1,  -1,   0;];
             
 

WiMax_ldpc_4d5B=[     -1, 432,  -1, 149,  -1,  -1,  74, 133,  90,  -1,  -1, 453, 154, 277, 416, 506, 117, 490,   0,   0,  -1,  -1,  -1,  -1;
                     224,  -1,  74, 362, 170,  -1,  -1,  -1,  -1, 373, 229,  58, 192, 213, 176, 304, 202, 128,  -1,   0,   0,  -1,  -1,  -1;
                      -1,  -1, 106,  -1,  -1, 336, 208,  -1, 373, 357,  -1, 202,  21, 384, 250, 154, 320,  26, 426,  -1,   0,   0,  -1,  -1;
                     341,  10,  -1,  -1, 336,  -1,  -1,  16, 272,  -1, 432,  80, 501,  48, 453, 192,  74, 101,  -1,  -1,  -1,   0,   0,  -1;
                      -1, 282, 320, 426,  -1, 138, 400,  -1,  -1,  -1,  -1, 458, 410,   5,  16, 384, 320, 133,  -1,  -1,  -1,  -1,   0,   0;
                     410,  -1,  -1,  -1,  80, 149,  -1, 186,  -1, 384, 160, 362, 453, 448, 138, 341,  58, 474,   0,  -1,  -1,  -1,  -1,   0;];
ZWiMax_ldpc_4d5B = 96;

%%
H_c = hmx_2d3B_n2;
z_max = 512;
Z1 = 360;
Z2 = 180;
Z3 = 90;
Z4 = 45;
g4 = 0;
g6 = 0;
g4_pos = zeros(1000, 8);
g6_pos = zeros(3000,12);
[M, N] = size(H_c);
K = N - M;
% H_c = zeros(M,N);                      %%%输出的Zmax=256的矩阵
H_c_b0 = H_c; 
H_c_b1 = -1.*ones(M,N);                  %%Z1=360的准循环移位矩阵
H_c_b2 = -1.*ones(M,N);                  %%Z2=180的准循环移位矩阵
H_c_b3 = -1.*ones(M,N);                  %%Z3=90的准循环移位矩阵
H_c_b4 = -1.*ones(M,N);                  %%Z4=45的准循环移位矩阵
zz = zeros(M,1);                         %%存储校验节点的度

g4s = 1000*ones(1,4);
g6s = 1000*ones(1,4);
set_g4 = [0 0 0 0];
set_g6 = [0 0 500 500];

for ii=2:1:M-1
    if (H_c(ii, K+1)>=0)
        xpos = ii;
        break;
    end
end

for ii=1:1:M
    for jj=1:1:N
        if (H_c_b0(ii,jj)>=0)
            H_c_b1(ii,jj) = floor((H_c_b0(ii,jj)*Z1)/z_max);
            H_c_b2(ii,jj) = floor((H_c_b0(ii,jj)*Z2)/z_max);
            H_c_b3(ii,jj) = floor((H_c_b0(ii,jj)*Z3)/z_max);
            H_c_b4(ii,jj) = floor((H_c_b0(ii,jj)*Z4)/z_max);
        else
            H_c_b1(ii,jj) = H_c(ii,jj);
            H_c_b2(ii,jj) = H_c(ii,jj);
            H_c_b3(ii,jj) = H_c(ii,jj);
            H_c_b4(ii,jj) = H_c(ii,jj);
        end
    end
end

for ii=1:1:M
    for jj=1:1:N
        fprintf('%4d,', H_c_b1(ii,jj));
    end
    fprintf('\n');
end
fprintf('\n');
for ii=1:1:M
    for jj=1:1:N
        fprintf('%4d,', H_c_b2(ii,jj));
    end
    fprintf('\n');
end
fprintf('\n');
for ii=1:1:M
    for jj=1:1:N
        fprintf('%4d,', H_c_b3(ii,jj));
    end
    fprintf('\n');
end
fprintf('\n');
for ii=1:1:M
    for jj=1:1:N
        fprintf('%4d,', H_c_b4(ii,jj));
    end
    fprintf('\n');
end

[g4,g6,g4_pos,g6_pos,g4s(1),g6s(1)] = my_girth_4_6(H_c_b1,Z1,z_max,g4,g6,g4_pos,g6_pos, 1);
[g4,g6,g4_pos,g6_pos,g4s(2),g6s(2)] = my_girth_4_6(H_c_b2,Z2,z_max,g4,g6,g4_pos,g6_pos, 0);
[g4,g6,g4_pos,g6_pos,g4s(3),g6s(3)] = my_girth_4_6(H_c_b3,Z3,z_max,g4,g6,g4_pos,g6_pos, 0);
[g4,g6,g4_pos,g6_pos,g4s(4),g6s(4)] = my_girth_4_6(H_c_b4,Z4,z_max,g4,g6,g4_pos,g6_pos, 0);

 
 while (1)
    if (girth_cmp(g4s, set_g4)==1 && girth_cmp(g6s, set_g6)==1)                        %%%无4,6环，输出结果
        for ii=1:1:M
            for jj=1:1:N
               fprintf('%4d',H_c_b0(ii,jj));
            end
            fprintf('\n');
        end
        fprintf('\n');
        break;
    end
    
    %%消6环
    while(1)
        if (girth_cmp(g4s, set_g4)==0)
            break;                         %%有4环，先消4环
        end
        if (girth_cmp(g6s, set_g6)==1)                         %%无6环，跳出
            break;
        end
        if (girth_cmp(g6s, set_g6)==0)                    
            for ii=1:1:M
                for jj=1:1:N
                    if (H_c_b1(ii,jj)>=0)
                        zz(ii)=zz(ii)+1;              %%计算校验节点的度
                    end
                end
            end
        
                            
            for ii=1:1:g6
               gogo = (mod((H_c_b1(g6_pos(ii,1),g6_pos(ii,2)) - H_c_b1(g6_pos(ii,3),g6_pos(ii,4)) + H_c_b1(g6_pos(ii,5),g6_pos(ii,6)) -  ...
                               H_c_b1(g6_pos(ii,7),g6_pos(ii,8)) + H_c_b1(g6_pos(ii,9),g6_pos(ii,10)) - H_c_b1(g6_pos(ii,11),g6_pos(ii,12))),Z1)==0) || ...
                          (mod((H_c_b2(g6_pos(ii,1),g6_pos(ii,2)) - H_c_b2(g6_pos(ii,3),g6_pos(ii,4)) + H_c_b2(g6_pos(ii,5),g6_pos(ii,6)) -  ...
                               H_c_b2(g6_pos(ii,7),g6_pos(ii,8)) + H_c_b2(g6_pos(ii,9),g6_pos(ii,10)) - H_c_b2(g6_pos(ii,11),g6_pos(ii,12))),Z2)==0) || ...
                          (mod((H_c_b3(g6_pos(ii,1),g6_pos(ii,2)) - H_c_b3(g6_pos(ii,3),g6_pos(ii,4)) + H_c_b3(g6_pos(ii,5),g6_pos(ii,6)) -  ...
                               H_c_b3(g6_pos(ii,7),g6_pos(ii,8)) + H_c_b3(g6_pos(ii,9),g6_pos(ii,10)) - H_c_b3(g6_pos(ii,11),g6_pos(ii,12))),Z3)==0) || ...
                          (mod((H_c_b4(g6_pos(ii,1),g6_pos(ii,2)) - H_c_b4(g6_pos(ii,3),g6_pos(ii,4)) + H_c_b4(g6_pos(ii,5),g6_pos(ii,6)) -  ...
                               H_c_b4(g6_pos(ii,7),g6_pos(ii,8)) + H_c_b4(g6_pos(ii,9),g6_pos(ii,10)) - H_c_b4(g6_pos(ii,11),g6_pos(ii,12))),Z4)==0);
               while(gogo)
                      if (zz(g6_pos(ii,1))>zz(g6_pos(ii,5)))
                          if (zz(g6_pos(ii,5))>zz(g6_pos(ii,9)))                %%改变校验节点度小的Z
                              ll = 9;
                              oo = 5;
                          else
                              ll = 5;
                              oo = 9;
                          end
                      else
                          if (zz(g6_pos(ii,1))>zz(g6_pos(ii,9)))
                              ll = 9;
                              oo = 1;
                          else
                              ll = 1;
                              oo = 9;
                          end
                      end
                      if (g6_pos(ii,ll+1)>K)
                           ll = ll + 2;
                      else if (g6_pos(ii,ll+1)>K)
                               ll = oo;
                          end
                      end
                      

                      H_c_b0(g6_pos(ii,ll),g6_pos(ii,ll+1)) = fix(z_max*rand);
                      H_c_b1(g6_pos(ii,ll),g6_pos(ii,ll+1)) = floor(H_c_b0(g6_pos(ii,ll),g6_pos(ii,ll+1))*Z1/z_max);
                      H_c_b2(g6_pos(ii,ll),g6_pos(ii,ll+1)) = floor(H_c_b0(g6_pos(ii,ll),g6_pos(ii,ll+1))*Z2/z_max);
                      H_c_b3(g6_pos(ii,ll),g6_pos(ii,ll+1)) = floor(H_c_b0(g6_pos(ii,ll),g6_pos(ii,ll+1))*Z3/z_max);
                      H_c_b4(g6_pos(ii,ll),g6_pos(ii,ll+1)) = floor(H_c_b0(g6_pos(ii,ll),g6_pos(ii,ll+1))*Z4/z_max);
%                       if (H_c_b(1,17)~=gg)
%                            
%                       end
        
                      if ((mod((H_c_b1(g6_pos(ii,1),g6_pos(ii,2)) - H_c_b1(g6_pos(ii,3),g6_pos(ii,4)) + H_c_b1(g6_pos(ii,5),g6_pos(ii,6)) -  ...
                               H_c_b1(g6_pos(ii,7),g6_pos(ii,8)) + H_c_b1(g6_pos(ii,9),g6_pos(ii,10)) - H_c_b1(g6_pos(ii,11),g6_pos(ii,12))),Z1)==0) || ...
                          (mod((H_c_b2(g6_pos(ii,1),g6_pos(ii,2)) - H_c_b2(g6_pos(ii,3),g6_pos(ii,4)) + H_c_b2(g6_pos(ii,5),g6_pos(ii,6)) -  ...
                               H_c_b2(g6_pos(ii,7),g6_pos(ii,8)) + H_c_b2(g6_pos(ii,9),g6_pos(ii,10)) - H_c_b2(g6_pos(ii,11),g6_pos(ii,12))),Z2)==0) || ...
                          (mod((H_c_b3(g6_pos(ii,1),g6_pos(ii,2)) - H_c_b3(g6_pos(ii,3),g6_pos(ii,4)) + H_c_b3(g6_pos(ii,5),g6_pos(ii,6)) -  ...
                               H_c_b3(g6_pos(ii,7),g6_pos(ii,8)) + H_c_b3(g6_pos(ii,9),g6_pos(ii,10)) - H_c_b3(g6_pos(ii,11),g6_pos(ii,12))),Z3)==0) || ...
                          (mod((H_c_b4(g6_pos(ii,1),g6_pos(ii,2)) - H_c_b4(g6_pos(ii,3),g6_pos(ii,4)) + H_c_b4(g6_pos(ii,5),g6_pos(ii,6)) -  ...
                               H_c_b4(g6_pos(ii,7),g6_pos(ii,8)) + H_c_b4(g6_pos(ii,9),g6_pos(ii,10)) - H_c_b4(g6_pos(ii,11),g6_pos(ii,12))),Z4)==0));
                          continue;
                      else
                          break;
                      end
               end
            end
         end
         [g4,g6,g4_pos,g6_pos,g4s(1),g6s(1)] = my_girth_4_6(H_c_b1,Z1,z_max,g4,g6,g4_pos,g6_pos, 1);
         [g4,g6,g4_pos,g6_pos,g4s(2),g6s(2)] = my_girth_4_6(H_c_b2,Z2,z_max,g4,g6,g4_pos,g6_pos, 0);
         [g4,g6,g4_pos,g6_pos,g4s(3),g6s(3)] = my_girth_4_6(H_c_b3,Z3,z_max,g4,g6,g4_pos,g6_pos, 0);
         [g4,g6,g4_pos,g6_pos,g4s(4),g6s(4)] = my_girth_4_6(H_c_b4,Z4,z_max,g4,g6,g4_pos,g6_pos, 0);
     
    end
   
    %%消4环
    while(1)
        if (girth_cmp(g4s, set_g4)==1)                   %%无4环，跳出
            break;
        end
        if (girth_cmp(g4s, set_g4)==0)                      
            for ii=1:1:M
                for jj=1:1:N
                    if (H_c_b1(ii,jj)>=0)
                        zz(ii)=zz(ii)+1;     %%计算校验节点的度     
                    end
                end
            end                     
            for ii=1:1:g4
               while(1)
                      if (zz(g4_pos(ii,1)) > zz(g4_pos(ii,5)))               %%%%改变校验节点度小的Z
                          ll = 5;
                      else
                          ll = 1;
                      end
                      if(g4_pos(ii,ll+1)>K)
                          ll=ll+2;
                      end
                          
                      H_c_b0(g4_pos(ii,ll),g4_pos(ii,ll+1)) = fix(z_max*rand);
                      H_c_b1(g4_pos(ii,ll),g4_pos(ii,ll+1)) = floor(H_c_b0(g4_pos(ii,ll),g4_pos(ii,ll+1))*Z1/z_max);
                      H_c_b2(g4_pos(ii,ll),g4_pos(ii,ll+1)) = floor(H_c_b0(g4_pos(ii,ll),g4_pos(ii,ll+1))*Z2/z_max);
                      H_c_b3(g4_pos(ii,ll),g4_pos(ii,ll+1)) = floor(H_c_b0(g4_pos(ii,ll),g4_pos(ii,ll+1))*Z3/z_max);
                      H_c_b4(g4_pos(ii,ll),g4_pos(ii,ll+1)) = floor(H_c_b0(g4_pos(ii,ll),g4_pos(ii,ll+1))*Z4/z_max);
                      
                      if ((mod((H_c_b1(g4_pos(ii,1),g4_pos(ii,2)) - H_c_b1(g4_pos(ii,3),g4_pos(ii,4)) + H_c_b1(g4_pos(ii,5),g4_pos(ii,6)) - H_c_b1(g4_pos(ii,7),g4_pos(ii,8))),Z1)==0) || ...
                          (mod((H_c_b2(g4_pos(ii,1),g4_pos(ii,2)) - H_c_b2(g4_pos(ii,3),g4_pos(ii,4)) + H_c_b2(g4_pos(ii,5),g4_pos(ii,6)) - H_c_b2(g4_pos(ii,7),g4_pos(ii,8))),Z2)==0) || ...
                          (mod((H_c_b3(g4_pos(ii,1),g4_pos(ii,2)) - H_c_b3(g4_pos(ii,3),g4_pos(ii,4)) + H_c_b3(g4_pos(ii,5),g4_pos(ii,6)) - H_c_b3(g4_pos(ii,7),g4_pos(ii,8))),Z3)==0) || ...
                          (mod((H_c_b4(g4_pos(ii,1),g4_pos(ii,2)) - H_c_b4(g4_pos(ii,3),g4_pos(ii,4)) + H_c_b4(g4_pos(ii,5),g4_pos(ii,6)) - H_c_b4(g4_pos(ii,7),g4_pos(ii,8))),Z4)==0));
                          continue;
                      else
                          break;
                      end
               end
            end
        end
       [g4,g6,g4_pos,g6_pos,g4s(1),g6s(1)] = my_girth_4_6(H_c_b1,Z1,z_max,g4,g6,g4_pos,g6_pos, 1);
       [g4,g6,g4_pos,g6_pos,g4s(2),g6s(2)] = my_girth_4_6(H_c_b2,Z2,z_max,g4,g6,g4_pos,g6_pos, 0);
       [g4,g6,g4_pos,g6_pos,g4s(3),g6s(3)] = my_girth_4_6(H_c_b3,Z3,z_max,g4,g6,g4_pos,g6_pos, 0);
       [g4,g6,g4_pos,g6_pos,g4s(4),g6s(4)] = my_girth_4_6(H_c_b4,Z4,z_max,g4,g6,g4_pos,g6_pos, 0);
    end
 end


    
        
        

            
            
 
          
        
       
    
    





